import { Route, Redirect } from 'react-router-dom'
import { isAuth } from '@/utils'

// 创建自定义鉴定路由
export const AuthRoute = ({ component: Component, ...rest }) => {
  return (
    <Route
      // 将剩余参数传入
      {...rest}
      // 利用render方法自定义渲染路由
      render={(props) => {
        // 判断是否有token 有渲染当前路由 无返回登陆页面
        if (isAuth()) return <Component />
        return <Redirect to={{
          pathname: 'login',
          state: {
            from: props.location.pathname
          }
        }} />
      }}
    />
  )
}
